import * as apis from '../services/examinationService.js'

export default {
    namespace:'examination',

    state:{
        technologyExaminationTableData:[],
        mechanismList:[]
    },

    reducers:{
        setTechnologyExaminationTableData:setTechnologyExaminationTableData,
        setMechanismList:setMechanismList
    },

    effects:{
       getMechanismList:getMechanismList,
       getTechnologyExaminationTableData:getTechnologyExaminationTableData,
       updateExamination:updateExamination,
       updateMechanismExamination:updateMechanismExamination,
       updateFileExamination:updateFileExamination
    }
}

function setMechanismList(state,payload){
    let _state = JSON.parse(JSON.stringify(state));
    _state.mechanismList = payload.data
    return _state
}

function setTechnologyExaminationTableData(state,payload){
    let _state = JSON.parse(JSON.stringify(state));
    _state. technologyExaminationTableData = payload.data
    return _state
}

/**
 * 修改机构的审核状态
 */
function* updateMechanismExamination(payload,{select,call,put}){
    let result = yield call(apis.updateMechanismExamination,payload.data)

    yield put({
        type:"app/setServerRes",
        data:result.data
    })
}

/**
 * 修改技术申请资格审核的审核状态
 */
function* updateExamination(payload, { select, call ,put}){
    let result = yield call(apis.updateExamination,payload.data)
    
    yield put({
        type:"app/setServerRes",
        data:result.data
    })
}

/**
 * 修改技术申请各项文件的审核状态
 */
function* updateFileExamination(payload, {select,call,put}){
    let result = yield call(apis.updateFileExamination,payload.data)

    yield put({
        type:"app/setServerRes",
        data:result.data
    })
}


/**
 * 获取所有机构列表
 */
function* getMechanismList(payload,{ select, call, put}){
    let result = yield call(apis.getMechanismList)
    for(let i=0;i<result.data.data.length;i++){
        for(let key in result.data.data[i]){
            if(key == "exminationStatus"){
                if(result.data.data[i][key] == 0){
                    result.data.data[i][key] = "审核未通过"
                }
                if(result.data.data[i][key] == 1){
                    result.data.data[i][key] = "待审核"
                }
                if(result.data.data[i][key] == 0){
                    result.data.data[i][key] = "审核通过"
                }
            }

            if(key == "mechanismType"){
                if(result.data.data[i][key] == 1){
                    result.data.data[i][key] = "验证机构"
                }

                if(result.data.data[i][key] == 2){
                    result.data.data[i][key] = "测试机构"
                }
            }

            if(key == "MechanismExamination"){
                result.data.data[i].examinationStatus = result.data.data[i][key].examinationStatus == 0 ? "审核未通过" : (result.data.data[i][key].examinationStatus == 1 ? "待审核" : "审核通过")
            }
        }
    }
    yield put({
        type:"examination/setMechanismList",
        data:result.data.data
    })
}

/**
 * 获取所有技术申请信息及状态和阶段
 */
function* getTechnologyExaminationTableData(payload,{ select, call, put}){
    let result = yield call(apis.getTechnologyExaminationTableData)
    let dataArr = []
    yield put({
        type:"app/initServerRes"
    })
    for(let i=0;i<result.data.data.length;i++){
        let data = {}
        let technologyInformation = {}
        for(let key in result.data.data[i]){      
            if(key == "id"){
                data.id = result.data.data[i][key]
            }

            if(key == "technologyName"){
                data.technologyName = result.data.data[i][key]
            }

            if(key == "createdAt"){
                data.createdAt = result.data.data[i][key].substring(0,10)
            }

            if(key == "companyAddress"){
                technologyInformation.companyAddress = result.data.data[i][key]
            }

            if(key == "companyName"){
                technologyInformation.companyName = result.data.data[i][key]
            }

            if(key == "companyNature"){
                technologyInformation.companyNature = result.data.data[i][key]
            }

            if(key == "contacts"){
                technologyInformation.contacts = result.data.data[i][key]
            }

            if(key == "email"){
                technologyInformation.email = result.data.data[i][key]
            }

            if(key == "isStatue"){
                if(result.data.data[i][key] == 0){
                    technologyInformation.isStatue = "否"
                }

                if(result.data.data[i][key] == 1){
                    technologyInformation.isStatue = "是"
                }
            }

            if(key == "idNumber"){
                technologyInformation.idNumber = result.data.data[i][key]
            }

            if(key == "innovationDescription"){
                technologyInformation.innovationDescription = result.data.data[i][key]
            }

            if(key == "legalRepresentative"){
                technologyInformation.legalRepresentative = result.data.data[i][key]
            }

            if(key == "operatingConditions"){
                technologyInformation.operatingConditions = result.data.data[i][key]
            }

            if(key == "phone"){
                technologyInformation.phone = result.data.data[i][key]
            }

            if(key == "postalCode"){
                technologyInformation.postalCode = result.data.data[i][key]
            }

            if(key == "referenceMaterial"){
                technologyInformation.referenceMaterial = result.data.data[i][key]
            }

            if(key == "regulation"){
                technologyInformation.regulation = result.data.data[i][key]
            }

            if(key == "socialCreditCode"){
                technologyInformation.socialCreditCode = result.data.data[i][key]
            }

            if(key == "technicalStandard"){
                technologyInformation.technicalStandard = result.data.data[i][key]
            }

            if(key == "technologyAlternatives"){
                technologyInformation.technologyAlternatives = result.data.data[i][key]
            }

            if(key == "technologyName"){
                technologyInformation.technologyName = result.data.data[i][key]
            }

            if(key == "technologyPrinciple"){
                technologyInformation.technologyPrinciple = result.data.data[i][key]
            }

            if(key == "technologyTarget"){
                technologyInformation.technologyTarget = result.data.data[i][key]
            }

            if(key == "TechnologyExaminations"){
                data.TechnologyExamination = result.data.data[i][key]
            }

            if(key == "TestFile" && result.data.data[i].TestFile != null){
               data.testReport = result.data.data[i][key].testReport
            }

            if(key == "VerificationFile" && result.data.data[i].VerificationFile != null){
                data.verificationReport = result.data.data[i][key].verificationReport
                data.verificationStatement = result.data.data[i][key].verificationStatement
            }
        }
        data.technologyInformation = technologyInformation
        dataArr.push(data)
    }
    yield put({
        type:"examination/setTechnologyExaminationTableData",
        data:dataArr
    })
}

